/****************************************************
 * 													*
 * 					BanzaiP2P						*
 * 													*
 * Versione: 0.1.4									*
 * Autore: D'Annessa Marco							*
 * File: servent.c									*
 ****************************************************/

#include "servent.h"


void serv_start( ){
	
	pthread_t tconnected[conf->maxcon_incoming];
	
	

	lsock = socket(AF_INET,SOCK_STREAM,0);
	int reuse=1;
	setsockopt(lsock,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(int));
	
	
	memset((char*)&lsaddr,0,sizeof(lsaddr));printf("\nport %d\n",conf->port);
	lsaddr.sin_port = htons(conf->port);
	lsaddr.sin_addr.s_addr = htonl(INADDR_ANY);

	
	
	bind(lsock, (struct sockaddr*)&lsaddr, sizeof(lsaddr));
	listen(lsock,5);
	printf("\nserver partito\n");

 
	for(;;){
		printf("\ciclo\n");
		
		if((connected=accept(lsock,NULL,NULL))<0)
			printf("\nerrore nella connesione\n");
		
		else{			
			count++;
			printf("\nconnesione stabilita: %d\n",count);
			
			pthread_create(&tconnected[tcount-1],NULL, service,(void*)connected);
		}
	
		
							
		
	}
	
	
}


void service(void *con){
	int conn_sock=(int)con;
	struct http_rqst_msg msg;
	struct http_rspn_msg rspn;
	printf("\nNew service started: %d\n",conn_sock);
	
	
/*
	struct file_list* hit,*h;
	struct file_list* mem=shared_files;
	struct token *query;
	char str[128];
	char str_ent[2048];
	int len,x=0;*/
	char str_ent[2048];
	
	for(;;){		
		
		http_rqst_msg_init(&msg,"0","7");
		http_rspn_msg_init(&rspn,"0","7");
		
		http_recv_rqst(conn_sock, &msg);

		int str_ent_len;
		str_ent_len = rcv_query(&msg, str_ent);
		
		sprintf(rspn.ent_header.cont_len,"%d",str_ent_len+1);
		rspn.entity=str_ent;
		
		strcpy(rspn.response.code,http_rspn_code_string[HTTP_STR_RSPN_CODE_200]);
		strcpy(rspn.response.reason,http_rspn_reason_string[HTTP_STR_RSPN_CODE_200]);
		
		
		http_send_rspn(conn_sock,&rspn);
		
		
		printf("\nS %d\n",conn_sock);
	}

		
	
}

int rcv_query(struct http_rqst_msg* msg, char* str_ent){

	struct file_list* hit,*h;
	struct file_list* mem=shared_files;
	struct token *query;
	char str[128],temp[128];
	int len=0,k;
	
	sscanf(msg->ent_header.cont_len,"%d",&len);

	sprintf(str,"%s","");
	for(k=0;k<len-1;k++){
		str[k]=msg->entity[k];
	}
	str[len-1]='\0';
	
	//printf("\nSTR: %s",str);
	//query
		
		
	query = make_token(str);
	
	h=hit = search_file(shared_files, query);
	shared_files=mem;
	
		
	sprintf(str_ent,"%s","");
	while(hit->next!=NULL){
		
		/*strcat(str_ent,hit->file->name);
		strcat(str_ent,",");
		
		strcat(str_ent,hit->file->type);
		strcat(str_ent,",");
		
		sprintf(size,"%d",hit->file->size);
		strcat(str,size);
		strcat(str_ent,";");*/
		sprintf(temp,"%s,%s,%d;",hit->file->name,hit->file->type,hit->file->size);
		strcat(str_ent,temp);
		strcpy(temp,"\n");
		printf("s %s\n",str_ent);

		hit=hit->next;
	}
	strcat(str_ent,"#");
		
	
	hit=h;		
	
	while(hit->next!=NULL){
		free(hit->file->name);
		free(hit->file);
		//free(hit->token_file_name);
		hit=hit->next;
	}
	free(hit);
	

	return strlen(str_ent);

};

